package wensheng.stu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import wensheng.stu.common.R;
import wensheng.stu.dto.DishDto;
import wensheng.stu.entity.Category;
import wensheng.stu.entity.Dish;
import wensheng.stu.entity.DishFlavor;
import wensheng.stu.service.CategoryService;
import wensheng.stu.service.DishFlavorService;
import wensheng.stu.service.DishService;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){

        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);

        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page , int pageSize, String name){
        Page<Dish> dishPage = new Page<Dish>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();


        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(name!=null,Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(dishPage,queryWrapper);

        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<Dish> records = dishPage.getRecords();

        List<DishDto> list =  records.stream().map((item)->{

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if(category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());

        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage) ;
    }

    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){

        DishDto disDto = dishService.getByIdWithFlavor(id);


        return R.success(disDto);
    }

    @PutMapping
    @Transactional
    public R<String> update(@RequestBody DishDto dishDto){

        log.info(dishDto.toString());
        dishService.updateWithFlavor(dishDto);

        return R.success("修改菜品成功");
    }

    @DeleteMapping
    public R<String> delete(String ids){

        log.info("删除的dish的id为: {}",ids);
        List<Long> collect = Arrays.stream(ids.split(",")).map((item) -> {
            long l = Long.parseLong(item);
            return l;
        }).collect(Collectors.toList());
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,collect);

        dishService.remove(queryWrapper);

        return R.success("删除成功");

    }

    @PutMapping("/status/{st}")
    public R<String> updateStatus1(@PathVariable int sta, String ids){
        log.info("修改的dish的id为: {}",ids);
        List<Dish> collect = Arrays.stream(ids.split(",")).map((item) -> {
            long l = Long.parseLong(item);
            Dish dish = new Dish();
            dish.setId(l);
            dish.setStatus(sta);
            return dish;
        }).collect(Collectors.toList());
        dishService.updateBatchById(collect);
        return R.success("状态修改完成");

    }


    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){

        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        queryWrapper.eq(Dish::getStatus,1);

        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = dishService.list(queryWrapper);

        List<DishDto> collect = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();

            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);

            if (category != null) {
                String name = category.getName();
                dishDto.setCategoryName(name);
            }

            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DishFlavor::getDishId,id);
            List<DishFlavor> list1 = dishFlavorService.list(wrapper);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());


        return R.success(collect);
    }







}
